package com.castle.fortress.admin.cms.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.castle.fortress.admin.cms.dto.CmsConfigDto;
import com.castle.fortress.admin.cms.entity.CmsConfigEntity;
import com.castle.fortress.admin.cms.service.CmsConfigService;
import com.castle.fortress.admin.core.constants.GlobalConstants;
import com.castle.fortress.admin.utils.ExcelUtils;
import com.castle.fortress.common.entity.DynamicExcelEntity;
import com.castle.fortress.common.entity.RespBody;
import com.castle.fortress.common.exception.BizException;
import com.castle.fortress.common.respcode.GlobalRespCode;
import com.castle.fortress.common.utils.ConvertUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * cms通用配置表 控制器
 *
 * @author castle
 * @since 2022-02-18
 */
@Api(tags="cms通用配置表管理控制器")
@Controller
public class CmsConfigController {
    @Autowired
    private CmsConfigService cmsConfigService;

    /**
     * cms通用配置表的分页展示
     * @param cmsConfigDto cms通用配置表实体类
     * @param current 当前页
     * @param size  每页记录数
     * @return
     */
    @ApiOperation("cms通用配置表分页展示")
    @GetMapping("/cms/cmsConfig/page")
    @ResponseBody
    @RequiresPermissions("cms:cmsConfig:pageList")
    public RespBody<IPage<CmsConfigDto>> pageCmsConfig(CmsConfigDto cmsConfigDto, @RequestParam(required = false) Integer current, @RequestParam(required = false)Integer size){
        Integer pageIndex= current==null? GlobalConstants.DEFAULT_PAGE_INDEX:current;
        Integer pageSize= size==null? GlobalConstants.DEFAULT_PAGE_SIZE:size;
        Page<CmsConfigDto> page = new Page(pageIndex, pageSize);
        IPage<CmsConfigDto> pages = cmsConfigService.pageCmsConfig(page, cmsConfigDto);

        return RespBody.data(pages);
    }

    /**
     * cms通用配置表的列表展示
     * @param cmsConfigDto cms通用配置表实体类
     * @return
     */
    @ApiOperation("cms通用配置表列表展示")
    @GetMapping("/cms/cmsConfig/list")
    @ResponseBody
    public RespBody<List<CmsConfigDto>> listCmsConfig(CmsConfigDto cmsConfigDto){
        List<CmsConfigDto> list = cmsConfigService.listCmsConfig(cmsConfigDto);
        return RespBody.data(list);
    }


    /**
     * cms通用配置编辑
     * @param list
     * @return
     */
    @ApiOperation("cms通用配置编辑")
    @PostMapping("/cms/cmsConfig/listedit")
    @ResponseBody
    @RequiresPermissions("cms:cmsConfig:edit")
    public RespBody<String> updateCmsConfig(@RequestBody List<CmsConfigDto> list){
        if(list == null){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
        if(cmsConfigService.updateCmsConfigs(list)){
            return RespBody.data("保存成功");
        }else{
            return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
        }
    }

    /**
     * cms通用配置表保存
     * @param cmsConfigDto cms通用配置表实体类
     * @return
     */
    @ApiOperation("cms通用配置表保存")
    @PostMapping("/cms/cmsConfig/save")
    @ResponseBody
    @RequiresPermissions("cms:cmsConfig:save")
    public RespBody<String> saveCmsConfig(@RequestBody CmsConfigDto cmsConfigDto){
        if(cmsConfigDto == null ){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
        CmsConfigEntity cmsConfigEntity = ConvertUtil.transformObj(cmsConfigDto,CmsConfigEntity.class);
        if(cmsConfigService.save(cmsConfigEntity)){
            return RespBody.data("保存成功");
        }else{
            return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
        }
    }

    /**
     * cms通用配置表编辑
     * @param cmsConfigDto cms通用配置表实体类
     * @return
     */
    @ApiOperation("cms通用配置表编辑")
    @PostMapping("/cms/cmsConfig/edit")
    @ResponseBody
    @RequiresPermissions("cms:cmsConfig:edit")
    public RespBody<String> updateCmsConfig(@RequestBody CmsConfigDto cmsConfigDto){
        if(cmsConfigDto == null || cmsConfigDto.getId() == null || cmsConfigDto.getId().equals(0L)){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
        CmsConfigEntity cmsConfigEntity = ConvertUtil.transformObj(cmsConfigDto,CmsConfigEntity.class);
        if(cmsConfigService.updateById(cmsConfigEntity)){
            return RespBody.data("保存成功");
        }else{
            return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
        }
    }

    /**
     * cms通用配置表删除
     * @param id
     * @return
     */
    @ApiOperation("cms通用配置表删除")
    @PostMapping("/cms/cmsConfig/delete")
    @ResponseBody
    @RequiresPermissions("cms:cmsConfig:delete")
    public RespBody<String> deleteCmsConfig(@RequestParam Long id){
        if(id == null ){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
        if(cmsConfigService.removeById(id)) {
            return RespBody.data("保存成功");
        }else{
            return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
        }
    }


    /**
     * cms通用配置表批量删除
     * @param ids
     * @return
     */
    @ApiOperation("cms通用配置表批量删除")
    @PostMapping("/cms/cmsConfig/deleteBatch")
    @ResponseBody
    @RequiresPermissions("cms:cmsConfig:deleteBatch")
    public RespBody<String> deleteCmsConfigBatch(@RequestBody List<Long> ids){
        if(ids == null || ids.size()<1){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
        if(cmsConfigService.removeByIds(ids)) {
            return RespBody.data("保存成功");
        }else{
            return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
        }
    }

    /**
     * cms通用配置表详情
     * @param id cms通用配置表id
     * @return
     */
    @ApiOperation("cms通用配置表详情")
    @GetMapping("/cms/cmsConfig/info")
    @ResponseBody
    @RequiresPermissions("cms:cmsConfig:info")
    public RespBody<CmsConfigDto> infoCmsConfig(@RequestParam Long id){
        if(id == null){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
        CmsConfigEntity cmsConfigEntity = cmsConfigService.getById(id);
        CmsConfigDto cmsConfigDto = ConvertUtil.transformObj(cmsConfigEntity,CmsConfigDto.class);

        return RespBody.data(cmsConfigDto);
    }

	/**
     * 动态表头导出 依据展示的字段导出对应报表
     * @param dynamicExcelEntity
     * @param response
     * @throws Exception
     */
	@PostMapping("/cms/cmsConfig/exportDynamic")
	@ApiOperation("动态表头导出，依据展示的字段导出对应报表")
	public void exportDynamic(@RequestBody DynamicExcelEntity<CmsConfigDto> dynamicExcelEntity, HttpServletResponse response) throws Exception {
		List<CmsConfigDto> list = cmsConfigService.listCmsConfig(dynamicExcelEntity.getDto());
		//字典、枚举、接口、json常量等转义后的列表数据 根据实际情况初始化该对象，null为list数据直接导出
		List<List<Object>> dataList = null;
		/**
        * 根据实际情况初始化dataList,可参照 com.castle.fortress.admin.system.controller.TmpDemoController类中方法：exportDynamic
        */
		ExcelUtils.exportDynamic(response,dynamicExcelEntity.getFileName()+".xlsx",null,list,dynamicExcelEntity.getHeaderList(),dataList);
	}


}
